class Api::BatchLineItemsController < ApplicationController
    skip_before_filter  :verify_authenticity_token
    def register
        batch_id=params[:batch_id]     
        session_token=params[:session_token]
        manufacture_uid = params[:card_manufacture]         
        card_account_no=params[:card_account_no]
        client_id=""      

        check_member= Member.authenticate_session_token(session_token)
        if !check_member.nil?         
            encrypt_key=""
            asset_identity=""

            find_batch = Batch.find_by_id(batch_id)
            if !find_batch.nil?
                card_type=find_batch.adult_or_child.upcase   
                product_category_id=find_batch.product_category_id
                find_product_category=ProductCategory.find_by_id(product_category_id) 

                if !find_product_category.nil? 
                    product_type=find_product_category.product_type_id

                    find_user=Client.find_by_id(find_batch.client_id)
                    if !find_user.nil?
                        encrypt_key=find_user.secrect_key    
                        location_code=find_user.location_code                               
                    
                        client_id=find_user.id
                        if !encrypt_key.nil?                               
                            check_manufacture = BatchLineItem.find_by_manufacture_uid(manufacture_uid)   
                            if check_manufacture.nil?
                                serial_no=BatchLineItem.generate_serial_no(client_id.to_s)

                                if product_type ==1 #CARD
                                    asset_identity=BatchLineItem.generate_account_no(location_code)
                                    @out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,"")                                
                                elsif product_type==2 #TICKET
                                    str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s              

                                    digest_data= Digest::MD5.hexdigest(str)                 
                                    hex_data=digest_data.hex            
                                    hex_str=hex_data.to_s
                                    asset_identity=hex_str[0..15]   

                                    check_asset=BatchLineItem.find_by_asset_identity(asset_identity)
                                            
                                    if check_asset.nil?                                       
                                       @out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type)                                  
                                    else
                                        @out=false,'Asset Identity  is already registered!'  
                                    end
                                end
                            else
                                if check_manufacture.asset_identity !=card_account_no
                                    @out=true,check_manufacture.serial_no,check_manufacture.asset_identity
                                else
                                   @out=false,'Manufacture UID  is already registered!'                              
                                end                                
                            end
                        else
                            @out=false,'Invalid User!'     
                        end
                    else
                        @out=false,"Client doesn't exist!"  
                    end
                else
                    @out=false,"Product Category doesn't exist!"
                end
            else
                @out=false,"Batch doesn't exist!"
            end
        else
            @out=false,'Sorry!Unauthorized user!'
        end
    end   

    def generate_card_account_items
        # Generate Code
        batch_id=params[:id]     
        client_id=""
        qty = params[:qty]   
        total_count=0   
        # check_member= Member.authenticate_session_token(session_token)
        # if !check_member.nil?         
            encrypt_key=""
            asset_identity=""

            find_batch = Batch.find_by_id(batch_id)
            if !find_batch.nil?
                find_batch.generated_qty = qty
                find_batch.save
                card_type=find_batch.adult_or_child.upcase   
                product_category_id=find_batch.product_category_id
                find_product_category=ProductCategory.find_by_id(product_category_id) 

                if !find_product_category.nil? 
                    product_type=find_product_category.product_type_id

                    find_user=Client.find_by_id(find_batch.client_id)
                    if !find_user.nil?
                        encrypt_key=find_user.secrect_key    
                        location_code=find_user.location_code                               
                    
                        client_id=find_user.id

                        if !encrypt_key.nil?  
                            arr = Array.new
                            # Generate with qty
                            qty.to_i.times do |i|
                                    serial_no=BatchLineItem.generate_serial_no(client_id.to_s)

                                    asset_identity=BatchLineItem.generate_account_no(location_code)
                                    security_code=BatchLineItem.generate_security_code
                                    out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,"","",security_code)                                
                                    item = out[2]
                                    total_count+=1
                                    arr.push(item)
                            end
                              @out =true,'Success',arr
                            # Generate with qty 

                        else
                            @out=false,'Invalid User!'     
                        end
                    else
                        @out=false,"Client doesn't exist!"  
                    end
                else
                    @out=false,"Product Category doesn't exist!"
                end
            else
                @out=false,"Batch doesn't exist!"
            end
        # else
        #     @out=false,'Sorry!Unauthorized user!'
        # end
        # Generate Code
    end 

    def update_manufacture_uid
        # account_no = params[:account_no]
        barcode = params[:barcode]
        manufacture_uid = params[:manufacture_uid]
        session_token = params[:session_token]
        batch_line_item = BatchLineItem.find_by_barcode(barcode)
        find_user = Member.find_by_session_token(session_token)
        if !find_user.nil? 
            if !batch_line_item.nil?
                if batch_line_item.manufacture_uid.nil?
                    batch_line_item.manufacture_uid = manufacture_uid
                    batch_line_item.save

                    # update batch
                    batch = Batch.find_by_id(batch_line_item.batch_id)
                    card_qty=batch.qty_processing.to_i
                    success_qty=batch.qty_success

                    batch.qty_processing=card_qty+1
                    batch.qty_success=success_qty+1
                    batch.save

                    @out = true,batch_line_item.asset_identity
                else
                    @out = false,"Duplicate Record"
                end
            else
                @out = false,"error"
            end
        else
            @out = false,"Not Authorized"
        end
    end 

    def get_item_by_batch
      batch_id = params[:batch_id]
      session_token = params[:session_token]
      get_batch = BatchLineItem.where("batch_id=?",batch_id)
      if !get_batch.nil?
        @out = true,get_batch
      else
        @out = false,"no batch"
      end
    end

    def code_activate
     session_token = params[:session_token]
     barcode =params[:barcode]
     find_seller = Member.find_by_session_token(session_token)
     if !find_seller.nil? 
         seller_id = find_seller.user_id
         find_barcode = BatchLineItem.find_by_barcode(barcode)
         if !find_barcode.nil?   
            find_batch = Batch.find_by_id(find_barcode.batch_id)
                if find_batch.export_to_seller_id == seller_id
                    if find_barcode.is_activated == false
                        find_barcode.is_activated = true
                        find_barcode.activated_date = DateTime.now
                        find_barcode.save
                        @out = true,"Success"
                    else
                        @out = false,"This is already activated!"
                    end
                else
                    @out = false,"Not Authorized Seller" 
                end
         else
            @out = false,"Fail"
         end
     else
        @out = false, "Not Authorized"
     end
    end


    def update_barcode_and_account_no
     session_token = params[:session_token]
     barcode =params[:barcode]
     user = Member.find_by_session_token(session_token)
     if !user.nil? 
         user_id = user.user_id
         find_barcode = BatchLineItem.find_by_barcode(barcode)
         if !find_barcode.nil?   
            @out = true,find_barcode
            
         else
            @out = false,"Invalid"
         end
     else
        @out = false, "Not Authorized"
     end
    end

    # def check_ticket
    #  session_token = params[:session_token]
    #  barcode =params[:barcode]
    #  find_seller = Member.find_by_session_token(session_token)
    #  if !find_seller.nil? 
    #      seller_id = find_seller.user_id
    #      find_barcode = BatchLineItem.find_by_barcode(barcode)
    #      if !find_barcode.nil? 
    #       @out = true,"valid"
    #      end
    #  else 
    #     @out = false, "Not Authorized"
    #  end
    # end

    def activated_list 
        session_token = params[:session_token]
        find_seller = Member.find_by_session_token(session_token)
        if !find_seller.nil?
            total_page_count= 0
            page_no = params[:page]
            if !page_no.nil? && page_no != ""
              offset = (page_no.to_i - 1 ) * 10
            else
              offset = 0
            end 
            seller_id = find_seller.user_id
            get_list = BatchLineItem.select("batch_line_items.id,batch_line_items.barcode,batch_line_items.activated_date").joins("join batches on batches.id=batch_line_items.batch_id").where("batch_line_items.is_activated=? and batches.export_to_seller_id=?",true,seller_id).limit(10).offset(offset)

            if get_list.count%10 > 0
              total_page_count = get_list.count/10 + 1
            else
              total_page_count = get_list.count/10
            end

            if !get_list.nil?
                @out = true, get_list, total_page_count
            else
                @out = false, "No data"
            end
        else
              @out = false, "Not Authorized"
        end

    end

    def seller_info
        session_token = params[:session_token]
        find_seller = Member.find_by_session_token(session_token)
        if !find_seller.nil?
            seller_id = find_seller.user_id
            total_export_qty = BatchLineItem.select("count(batch_line_items.id)as count").joins("join batches on batches.id=batch_line_items.batch_id").where("batches.export_to_seller_id=?",seller_id)
            total_activated_qty = BatchLineItem.select("count(batch_line_items.id)as count").joins("join batches on batches.id=batch_line_items.batch_id").where("batches.export_to_seller_id=? and batch_line_items.is_activated=?",seller_id,true)
            @out = true,total_export_qty[0]["count"],total_activated_qty[0]["count"]
        else
            @out = false,"Not Authorized"
        end
    end

end